package com.tcwlkj.uuService.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tcwlkj.uuService.MyApp;
import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.Message;
import com.tcwlkj.uuService.model.OrderModel;
import com.tcwlkj.uuService.model.UpdateMessage;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.CommomDialog;
import com.tcwlkj.uuService.utils.EventCenter;
import com.tcwlkj.uuService.utils.MyDialog;
import com.tcwlkj.uuService.utils.PreferencesUtils;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import retrofit2.Call;

/**
 * tv_start_addr
 * Created by Administrator on 2017/10/17 0017.
 */

public class WaitOrderAdapter extends RecyclerView.Adapter<WaitOrderAdapter.ViewHolder> {
    private ArrayList<OrderModel> modelList;
    private CommomDialog commomDialog;
    private Context context;

    private  Handler handler=new Handler(){
        @Override
        public void handleMessage(android.os.Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    notifyDataSetChanged();
                    break;
                case 1:
                    notifyDataSetChanged();
                    break;
            }
        }
    };
    private CommomDialog finishDialog;

    public WaitOrderAdapter(ArrayList<OrderModel> data, Context context) {
        this.modelList = data;
        this.context = context;
    }

    public void updateData(ArrayList<OrderModel> data) {
        this.modelList = data;
        notifyDataSetChanged();
    }
    @Override
    public WaitOrderAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ViewHolder viewHolder = new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_wait_order, parent, false));
        return viewHolder;
    }
    @Override
    public void onBindViewHolder(WaitOrderAdapter.ViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        if(!ChcheUtils.getInstance().getHashMap_int().containsKey(modelList.get(position).getID())){
            ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),0l);
        }
        if(!ChcheUtils.getInstance().getYZMhashMap().containsKey(modelList.get(position).getID())){
            ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
        }
        if(modelList.get(position).getHotBox().equals("0")){
            holder.tv_HotBox.setVisibility(View.GONE);
        }else {
            holder.tv_HotBox.setVisibility(View.VISIBLE);
        }
        switch (modelList.get(position).getOrderType()) {
            case "0"://实时
                holder.tv_state.setText("尽快送达");
                break;
            case "1"://预约
                String str="预约时间：<font color='#41b1fb'>"+modelList.get(position).getSubscribeTime()+"</font>";
                holder.tv_state.setText(Html.fromHtml(str));
                break;
            case "2"://指派
                holder.tv_state.setText("指派时间");
                break;
        }
        switch (modelList.get(position).getSendType()) {
            case "10"://送
                holder.tv_start_addr.setText(modelList.get(position).getBegShowAddress());
                holder.tv_start_name.setText("取：");
                holder.tv_end_name.setText("送：");
                holder.tv_order_type.setText("同城送");
                holder.tv_end_addr.setText(modelList.get(position).getEndShowAddress());
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_surplus_time.setVisibility(View.GONE);
                holder.tv_MorKM.setVisibility(View.VISIBLE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.ll_add_time.setVisibility(View.GONE);
                switch (modelList.get(position).getState()) {
                    case "2":
                        if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                            holder.btn_sumbit.setText("我已到达");
                        }else {
                            if(modelList.get(position).getUType().equals("1")){//用户
                                holder.btn_sumbit.setText("联系发货人");
                            }else {//商家
                                holder.btn_sumbit.setText("我已到达");
                            }
                        }
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "3":
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_sumbit.setText("拍照取货");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "6":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_sumbit.setText("我已送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                        holder.btn_cancel.setEnabled(false);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "7":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        switch (modelList.get(position).getUType()){
                            case "1"://用户
                                holder.btn_sumbit.setText("收货验证码");
                                if(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())<=0){
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                                    holder.btn_cancel.setEnabled(true);
                                    holder.btn_cancel.setText("重发验证码");
                                    if(ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID())!=null){
                                        ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID()).cancel();
                                        ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
                                    }
                                }else {
                                    if(ChcheUtils.getInstance().getYZMhashMap().get(modelList.get(position).getID())){
                                        times(modelList.get(position).getID());
                                    }
                                    holder.btn_cancel.setEnabled(false);
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                    holder.btn_cancel.setText(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())+"秒后重发");
                                }
                                break;
                            case "2"://商家
                                holder.btn_sumbit.setText("我已完成");
                                holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                holder.btn_cancel.setEnabled(false);
                                holder.btn_cancel.setText("取消订单");
                                break;
                        }
                        break;
                }
                break;
            case "20"://取
                holder.tv_start_addr.setText(modelList.get(position).getBegShowAddress());
                holder.tv_start_name.setText("取：");
                holder.tv_end_name.setText("收：");
                holder.tv_order_type.setText("同城取");
                holder.tv_end_addr.setText(modelList.get(position).getEndShowAddress());
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_surplus_time.setVisibility(View.GONE);
                holder.tv_MorKM.setVisibility(View.VISIBLE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.ll_add_time.setVisibility(View.GONE);
                switch (modelList.get(position).getState()) {
                    case "2":
                        if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                            holder.btn_sumbit.setText("我已到达");
                        }else {
                            if(modelList.get(position).getUType().equals("1")){//用户
                                holder.btn_sumbit.setText("联系取货人");
                            }else {//商家
                                holder.btn_sumbit.setText("我已到达");
                            }
                        }
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "3":
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_sumbit.setText("拍照取货");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "6":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_sumbit.setText("我已送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                        holder.btn_cancel.setEnabled(false);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "7":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        switch (modelList.get(position).getUType()){
                            case "1"://用户
                                holder.btn_sumbit.setText("收货验证码");
                                if(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())<=0){
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                                    holder.btn_cancel.setEnabled(true);
                                    holder.btn_cancel.setText("重发验证码");
                                    if(ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID())!=null){
                                        ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID()).cancel();
                                        ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
                                    }
                                }else {
                                    if(ChcheUtils.getInstance().getYZMhashMap().get(modelList.get(position).getID())){
                                        times(modelList.get(position).getID());
                                    }
                                    holder.btn_cancel.setEnabled(false);
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                    holder.btn_cancel.setText(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())+"秒后重发");
                                }
                                break;
                            case "2"://商家
                                holder.btn_sumbit.setText("我已完成");
                                holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                holder.btn_cancel.setEnabled(false);
                                holder.btn_cancel.setText("取消订单");
                                break;
                        }
                        break;
                }
                break;
            case "30"://买
                if(modelList.get(position).getSendSubType().equals("301")){//就近买
                    holder.tv_start_addr.setText("就近买(不限制购买地址)");
                }else {
                    holder.tv_start_addr.setText(modelList.get(position).getBegShowAddress());
                }
                holder.tv_start_name.setText("买：");
                holder.tv_end_name.setText("收：");
                holder.tv_order_type.setText("同城买");
                holder.tv_end_addr.setText(modelList.get(position).getEndShowAddress());
                holder.ll_start_distance.setVisibility(View.VISIBLE);
                holder.tv_end_distance.setVisibility(View.VISIBLE);
                holder.ll_surplus_time.setVisibility(View.GONE);
                holder.tv_MorKM.setVisibility(View.VISIBLE);
                holder.ll_add_time.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.VISIBLE);
                if(modelList.get(position).getPayGoodsMoney().equals("0.00")){
                    holder.tv_goods_money.setText("以小票为准");
                }else {
                    holder.tv_goods_money.setText(modelList.get(position).getPayGoodsMoney()+"元");
                }
                switch (modelList.get(position).getState()) {
                    case "2":
                        if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                            holder.btn_sumbit.setText("我已到达");
                        }else {
                            if(modelList.get(position).getUType().equals("1")){//用户
                                holder.btn_sumbit.setText("联系下单人");
                            }else {//商家
                                holder.btn_sumbit.setText("我已到达");
                            }
                        }
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "3":
                        holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                        holder.btn_sumbit.setText("拍照取货");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "6":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_sumbit.setText("我已送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                        holder.btn_cancel.setEnabled(false);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "7":
                        holder.tv_time.setText(modelList.get(position).getEstimateFinishTime()+"前送达");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        switch (modelList.get(position).getUType()){
                            case "1"://用户
                                holder.btn_sumbit.setText("收货验证码");
                                if(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())<=0){
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                                    holder.btn_cancel.setEnabled(true);
                                    holder.btn_cancel.setText("重发验证码");
                                    if(ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID())!=null){
                                        ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID()).cancel();
                                        ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
                                    }
                                }else {
                                    if(ChcheUtils.getInstance().getYZMhashMap().get(modelList.get(position).getID())){
                                        times(modelList.get(position).getID());
                                    }
                                    holder.btn_cancel.setEnabled(false);
                                    holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                    holder.btn_cancel.setText(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())+"秒后重发");
                                }
                                break;
                            case "2"://商家
                                holder.btn_sumbit.setText("我已完成");
                                holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                                holder.btn_cancel.setEnabled(false);
                                holder.btn_cancel.setText("取消订单");
                                break;
                        }
                }
                break;
            case "40"://帮
                holder.tv_start_addr.setText(modelList.get(position).getBegShowAddress());
                holder.tv_start_name.setText("帮忙地点:");
                holder.tv_end_name.setText("帮忙信息:");
                holder.tv_order_type.setText("同城帮");
                if(modelList.get(position).getHelpInfo().equals("")){
                    holder.tv_end_addr.setText("暂无");
                }else {
                    holder.tv_end_addr.setText(modelList.get(position).getHelpInfo() + "");
                }
                holder.tv_MorKM.setVisibility(View.INVISIBLE);
                holder.tv_end_distance.setVisibility(View.INVISIBLE);
                holder.ll_surplus_time.setVisibility(View.GONE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.ll_add_time.setVisibility(View.GONE);
                holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                switch (modelList.get(position).getState()) {
                    case "2":
                        if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                            holder.btn_sumbit.setText("我已到达");
                        }else {
                            holder.btn_sumbit.setText("联系下单人");
                        }
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "3":
                        holder.btn_sumbit.setText("帮忙完成");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        if(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())<=0){
                            holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                            holder.btn_cancel.setEnabled(true);
                            holder.btn_cancel.setText("重发验证码");
                            if(ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID())!=null){
                                ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID()).cancel();
                                ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
                            }
                        }else {
                            if(ChcheUtils.getInstance().getYZMhashMap().get(modelList.get(position).getID())){
                                times(modelList.get(position).getID());
                            }
                            holder.btn_cancel.setEnabled(false);
                            holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                            holder.btn_cancel.setText(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())+"秒后重发");
                        }
                        break;
                }
                break;
            case "50"://排
                holder.tv_start_addr.setText(modelList.get(position).getBegShowAddress());
                holder.tv_start_name.setText("排队地点:");
                holder.tv_end_name.setText("排队信息:");
                holder.tv_order_type.setText("同城排");
                if(modelList.get(position).getHelpInfo().equals("")){
                    holder.tv_end_addr.setText("暂无");
                }else {
                    holder.tv_end_addr.setText(modelList.get(position).getHelpInfo() + "");
                }
                holder.tv_MorKM.setVisibility(View.INVISIBLE);
                holder.tv_end_distance.setVisibility(View.INVISIBLE);
                holder.ll_surplus_time.setVisibility(View.GONE);
                holder.ll_add_time.setVisibility(View.VISIBLE);
                holder.ll_goods.setVisibility(View.GONE);
                holder.tv_paidui_time.setText(modelList.get(position).getExTimeSpan()+"分钟");
                holder.tv_time.setText(modelList.get(position).getEstimatePickTime()+"前到达");
                switch (modelList.get(position).getState()) {
                    case "2":
                        if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                            holder.btn_sumbit.setText("我已到达");
                        }else {
                            holder.btn_sumbit.setText("联系下单人");
                        }
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "3":
                        holder.btn_sumbit.setText("开始排队");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                        holder.btn_cancel.setEnabled(true);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "4":
                        holder.ll_surplus_time.setVisibility(View.VISIBLE);
                        time=(Long.parseLong(modelList.get(position).getExTimeSpan())*60*1000-(System.currentTimeMillis()-Tools.StringToDate(modelList.get(position).getState4Time()).getTime()))/1000;
                        if(flag){
                            time(position);
                        }
                        if(time<0){
                            holder.tv_fen.setText("0分钟");
                        }else {
                            holder.tv_fen.setText(hh+"小时"+mm+"分"+ss+"秒");
                        }
                        holder.btn_sumbit.setText("结束排队");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                        holder.btn_cancel.setEnabled(false);
                        holder.btn_cancel.setText("取消订单");
                        break;
                    case "7":
                        holder.btn_sumbit.setText("排队完成");
                        holder.btn_cancel.setVisibility(View.VISIBLE);
                        if(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())<=0){
                            holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.text_black));
                            holder.btn_cancel.setEnabled(true);
                            holder.btn_cancel.setText("重发验证码");
                            if(ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID())!=null){
                                ChcheUtils.getInstance().getHashMap_time().get(modelList.get(position).getID()).cancel();
                                ChcheUtils.getInstance().getYZMhashMap().put(modelList.get(position).getID(),true);
                            }
                        }else {
                            if(ChcheUtils.getInstance().getYZMhashMap().get(modelList.get(position).getID())){
                                times(modelList.get(position).getID());
                            }
                            holder.btn_cancel.setEnabled(false);
                            holder.btn_cancel.setTextColor(context.getResources().getColor(R.color.divider));
                            holder.btn_cancel.setText(ChcheUtils.getInstance().getHashMap_int().get(modelList.get(position).getID())+"秒后重发");
                        }
                        break;
                }
                break;
        }
        holder.tv_goods_type.setText(modelList.get(position).getQuickOpers());
        holder.tv_total_money.setText("￥" + modelList.get(position).getDriverFreightMoney());

        if (Double.valueOf(modelList.get(position).getDTakeDist()) < 1000) {
            holder.tv_start_distace.setText(Math.ceil(Double.valueOf(modelList.get(position).getDTakeDist())) + "m");
        } else {
            holder.tv_start_distace.setText(new java.text.DecimalFormat("#.00").format(Double.valueOf(modelList.get(position).getDTakeDist()) / 1000) + "km");
        }
        holder.tv_end_distance.setText(modelList.get(position).getDistance());
        if(modelList.get(position).getPayUserFeeMoney().equals("0.00元")){
            holder.ll_feemoney.setVisibility(View.GONE);
        }else {
            holder.ll_feemoney.setVisibility(View.VISIBLE);
            holder.tv_tip.setText(modelList.get(position).getPayUserFeeMoney());
        }
        if(modelList.get(position).getSysAddMoney().equals("0.00元")){
            holder.ll_addmoney.setVisibility(View.GONE);
        }else {
            holder.ll_addmoney.setVisibility(View.VISIBLE);
            holder.tv_add_money.setText(modelList.get(position).getSysAddMoney());
        }
        holder.tv_tip.setText(modelList.get(position).getPayUserFeeMoney());
        if(modelList.get(position).getRemarks().equals("")){
            holder.ll_remark.setVisibility(View.GONE);
        }else {
            holder.tv_remark.setText(modelList.get(position).getRemarks());
        }
        holder.tv_remark.setText(modelList.get(position).getRemarks());
        holder.btn_cancel.setOnClickListener(new View.OnClickListener() {//取消订单
            @Override
            public void onClick(View v) {
                switch (modelList.get(position).getSendType()){
                    case "10"://送
                        if(Integer.parseInt(modelList.get(position).getState())>=7){//重发验证码
                            new CommomDialog(context, R.style.dialog, "确定重发验证码吗?", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(999, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),60l);
                                }
                            }).show();
                        }else {
                            //取消订单
                            commomDialog = new CommomDialog(context, R.style.dialog, "跑男取消订单需扣2元", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    cancel(modelList.get(position).getID());//取消订单
                                }
                            });
                            commomDialog.setTitle("提示").show();
                            commomDialog.setvisibility(true);
                        }
                        break;
                    case "20"://取
                        if(Integer.parseInt(modelList.get(position).getState())>=7){//重发验证码
                            new CommomDialog(context, R.style.dialog, "确定重发验证码吗?", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(999, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),60l);
                                }
                            }).show();
                        }else {
                            //取消订单
                            commomDialog = new CommomDialog(context, R.style.dialog, "跑男取消订单需扣2元", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    cancel(modelList.get(position).getID());//取消订单
                                }
                            });
                            commomDialog.setTitle("提示").show();
                            commomDialog.setvisibility(true);
                        }
                        break;
                    case "30"://买
                        if(Integer.parseInt(modelList.get(position).getState())>=7){//重发验证码
                            new CommomDialog(context, R.style.dialog, "确定重发验证码吗?", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(999, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),60l);
                                }
                            }).show();
                        }else {
                            //取消订单
                            commomDialog = new CommomDialog(context, R.style.dialog, "跑男取消订单需扣2元", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    cancel(modelList.get(position).getID());//取消订单
                                }
                            });
                            commomDialog.setTitle("提示").show();
                            commomDialog.setvisibility(true);
                        }
                        break;
                    case "50"://排
                        if(Integer.parseInt(modelList.get(position).getState())>=7){//重发验证码
                            new CommomDialog(context, R.style.dialog, "确定重发验证码吗?", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(999, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),60l);
                                }
                            }).show();
                         }else {
                            //取消订单
                            commomDialog = new CommomDialog(context, R.style.dialog, "跑男取消订单需扣2元", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    cancel(modelList.get(position).getID());//取消订单
                                }
                            });
                            commomDialog.setTitle("提示").show();
                            commomDialog.setvisibility(true);
                        }
                        break;
                    case "40"://帮
                        if(Integer.parseInt(modelList.get(position).getState())>=3){//重发验证码
                            new CommomDialog(context, R.style.dialog, "确定重发验证码吗?", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(999, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    ChcheUtils.getInstance().getHashMap_int().put(modelList.get(position).getID(),60l);
                                }
                            }).show();
                        }else {
                            //取消订单
                            commomDialog = new CommomDialog(context, R.style.dialog, "跑男取消订单需扣2元", new CommomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    cancel(modelList.get(position).getID());//取消订单
                                }
                            });
                            commomDialog.setTitle("提示").show();
                            commomDialog.setvisibility(true);
                        }
                        break;
                }
            }
        });
        holder.btn_sumbit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (modelList.get(position).getSendType()) {
                    case "10":

                    case "20":

                    case "30"://送取买
                        switch (modelList.get(position).getState()) {
                            case "2":
                                if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                                    new CommomDialog(context, R.style.dialog, "确定我已到达吗?", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(444, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                        }
                                    }).show();
                                }else {
                                    if(modelList.get(position).getUType().equals("1")){//用户
                                        new CommomDialog(context, R.style.dialog, "确定拨打电话吗?", new CommomDialog.OnCloseListener() {
                                            @Override
                                            public void onClick(Dialog dialog, boolean confirm) {
                                                switch (modelList.get(position).getSendType()){
                                                    case "10"://送
                                                        UpdateMessage updateMessage=new  UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getID());
                                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(86, updateMessage));
                                                        break;
                                                    case "20"://取
                                                        UpdateMessage updateMessag=new  UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getID());
                                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(86, updateMessag));
                                                        break;
                                                    case "30"://买
                                                        UpdateMessage updateMessages=new  UpdateMessage(modelList.get(position).getEndUserMobile(), modelList.get(position).getID());
                                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(86, updateMessages));
                                                        break;
                                                }
                                            }
                                        }).show();
                                    }else {
                                        if(Tools.getDistance(Double.parseDouble(modelList.get(position).getBegLocLat()),Double.parseDouble(modelList.get(position).getBegLocLng()),ChcheUtils.getInstance().getLocationModel().getLat(),ChcheUtils.getInstance().getLocationModel().getLng())<500){
                                            new CommomDialog(context, R.style.dialog, "确定我已到达吗?", new CommomDialog.OnCloseListener() {
                                                @Override
                                                public void onClick(Dialog dialog, boolean confirm) {
                                                    EventBus.getDefault().post(new EventCenter<UpdateMessage>(444, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                                }
                                            }).show();
                                        }else {
                                            ToastUtil.getInstance().show("还未到达目的地");
                                        }
                                    }
                                }
                                break;
                            case "3":
                                new CommomDialog(context, R.style.dialog, "确定拍照取货吗?", new CommomDialog.OnCloseListener() {
                                    @Override
                                    public void onClick(Dialog dialog, boolean confirm) {
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(555, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    }
                                }).show();
                                break;
                            case "6":
                                new CommomDialog(context, R.style.dialog, "确定我已送达吗?", new CommomDialog.OnCloseListener() {
                                    @Override
                                    public void onClick(Dialog dialog, boolean confirm) {
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(666, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    }
                                }).show();
                                break;
                            case "7":
                                if(modelList.get(position).getUType().equals("1")){//用户
                                    MyDialog myDialog=new MyDialog(context,"YZM");
                                    myDialog.show();
                                    myDialog.setClicklistener(new MyDialog.ClickListenerInterface() {
                                        @Override
                                        public void doConfirm(String msg) {
                                            myDialog.dismiss();
                                            if(TextUtils.isEmpty(msg)){
                                                ToastUtil.getInstance().show("验证码不能为空");
                                                return;
                                            }
                                            UpdateMessage updateMessage=new  UpdateMessage(msg, modelList.get(position).getID());
                                            updateMessage.setLat(ChcheUtils.getInstance().getLocationModel().getLat());
                                            updateMessage.setLng(ChcheUtils.getInstance().getLocationModel().getLng());
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(777,updateMessage ));
                                        }
                                        @Override
                                        public void doCancel() {

                                        }
                                    });
                                }else if(modelList.get(position).getUType().equals("2")){//商家
                                    if(Tools.getDistance(Double.parseDouble(modelList.get(position).getEndLocLat()),Double.parseDouble(modelList.get(position).getEndLocLng()),ChcheUtils.getInstance().getLocationModel().getLat(),ChcheUtils.getInstance().getLocationModel().getLng())<500){
                                        new CommomDialog(context, R.style.dialog, "确定我已完成吗?", new CommomDialog.OnCloseListener() {
                                            @Override
                                            public void onClick(Dialog dialog, boolean confirm) {
                                                UpdateMessage updateMessage=new  UpdateMessage("", modelList.get(position).getID());
                                                updateMessage.setLat(ChcheUtils.getInstance().getLocationModel().getLat());
                                                updateMessage.setLng(ChcheUtils.getInstance().getLocationModel().getLat());
                                                EventBus.getDefault().post(new EventCenter<UpdateMessage>(777,updateMessage ));
                                            }
                                        }).show();
                                    }else {
                                        ToastUtil.getInstance().show("还未到达收货地，不能完成订单");
                                   }
                                }
                                break;
                        }
                        break;
                    case "40"://帮
                        switch (modelList.get(position).getState()) {
                            case "2":
                                if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                                    new CommomDialog(context, R.style.dialog, "确定我已到达吗?", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(444, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                        }
                                    }).show();
                                }else {
                                    new CommomDialog(context, R.style.dialog, "确定拨打电话吗?", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            UpdateMessage updateMessage=new  UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getID());
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(86, updateMessage));
                                        }
                                    }).show();
                                }
                                break;
                            case "3":

                                MyDialog myDialog=new MyDialog(context,"YZM");
                                myDialog.show();
                                myDialog.setClicklistener(new MyDialog.ClickListenerInterface() {
                                    @Override
                                    public void doConfirm(String msg) {
                                        myDialog.dismiss();
                                        if(TextUtils.isEmpty(msg)){
                                            ToastUtil.getInstance().show("验证码不能为空");
                                            return;
                                        }
                                        UpdateMessage updateMessage=new  UpdateMessage(msg, modelList.get(position).getID());
                                        updateMessage.setLat(ChcheUtils.getInstance().getLocationModel().getLat());
                                        updateMessage.setLng(ChcheUtils.getInstance().getLocationModel().getLng());
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(777,updateMessage ));
                                    }
                                    @Override
                                    public void doCancel() {

                                    }
                                });
                                break;
                        }
                        break;
                    case "50"://排
                        switch (modelList.get(position).getState()) {
                            case "2":
                                if(Integer.parseInt(modelList.get(position).getDUCallFlag())>0){
                                    new CommomDialog(context, R.style.dialog, "确定我已到达吗?", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(444, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                        }
                                    }).show();
                                }else {
                                    new CommomDialog(context, R.style.dialog, "确定拨打电话吗?", new CommomDialog.OnCloseListener() {
                                        @Override
                                        public void onClick(Dialog dialog, boolean confirm) {
                                            UpdateMessage updateMessage=new  UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getID());
                                            EventBus.getDefault().post(new EventCenter<UpdateMessage>(86, updateMessage));
                                        }
                                    }).show();
                                }
                                break;
                            case "3":
                                new CommomDialog(context, R.style.dialog, "确定开始排队吗?", new CommomDialog.OnCloseListener() {
                                    @Override
                                    public void onClick(Dialog dialog, boolean confirm) {
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(888, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    }
                                }).show();
                                break;
                            case "4":
                                new CommomDialog(context, R.style.dialog, "确定排队结束吗?", new CommomDialog.OnCloseListener() {
                                    @Override
                                    public void onClick(Dialog dialog, boolean confirm) {
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(666, new UpdateMessage(modelList.get(position).getUMobile(), modelList.get(position).getID())));
                                    }
                                }).show();
                                break;
                            case "7":
                                MyDialog myDialog=new MyDialog(context,"YZM");
                                myDialog.show();
                                myDialog.setClicklistener(new MyDialog.ClickListenerInterface() {
                                    @Override
                                    public void doConfirm(String msg) {
                                        myDialog.dismiss();
                                        if(TextUtils.isEmpty(msg)){
                                            ToastUtil.getInstance().show("验证码不能为空");
                                            return;
                                        }
                                        UpdateMessage updateMessage=new  UpdateMessage(msg, modelList.get(position).getID());
                                        updateMessage.setLat(ChcheUtils.getInstance().getLocationModel().getLat());
                                        updateMessage.setLng(ChcheUtils.getInstance().getLocationModel().getLng());
                                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(777,updateMessage ));
                                    }
                                    @Override
                                    public void doCancel() {

                                    }
                                });
                                break;
                        }
                        break;
                }
            }
        });
        holder.ll_location.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new EventCenter<OrderModel>(85, modelList.get(position)));
            }
        });
        holder.ll_phone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (modelList.get(position).getSendType()) {
                    case "10"://送
                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(80, new UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getEndUserMobile())));
                        break;
                    case "20"://取
                        EventBus.getDefault().post(new EventCenter<UpdateMessage>(81, new UpdateMessage(modelList.get(position).getBegUserMobile(), modelList.get(position).getEndUserMobile())));
                        break;
                    case "30"://买
                        EventBus.getDefault().post(new EventCenter<String>(82, modelList.get(position).getEndUserMobile()));
                        break;
                    case "40"://帮
                        EventBus.getDefault().post(new EventCenter<String>(83, modelList.get(position).getBegUserMobile()));
                        break;
                    case "50"://排
                        EventBus.getDefault().post(new EventCenter<String>(84, modelList.get(position).getBegUserMobile()));
                        break;
                }
            }
        });
    }
    private void cancel(String id) {
        if (TextUtils.isEmpty(commomDialog.getEditContent())) {
            ToastUtil.getInstance().show("取消原因不能为空");
            return;
        }
        Call<CommonResponse<Object>> httpResultCall = RetrofitUtils.getRequest(context, true).cancle(id, PreferencesUtils.getString(MyApp.getContext(), "userID"), commomDialog.getEditContent(), Tools.javabeanToJson(ChcheUtils.getInstance().getLocationModel()));
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<Object>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<Object> commonResponse = (CommonResponse<Object>) t;
                ToastUtil.getInstance().show(commonResponse.getMessage());
                EventBus.getDefault().post(new EventCenter<String>(222, "取消订单"));
            }
            @Override
            public void onFail(int code, String message) {
                if (code == 40104) {
                    cancel(id);
                } else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });
    }
    @Override
    public int getItemCount() {
        return modelList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private TextView tv_time;
        private TextView tv_total_money;
        private TextView tv_order_type;
        private TextView tv_state;
        private TextView tv_start_name;
        private TextView tv_end_name;
        private TextView tv_start_distace;
        private TextView tv_end_distance;
        private TextView tv_start_addr;
        private TextView tv_end_addr;
        private TextView tv_remark;
        private LinearLayout ll_add_time;
        private TextView tv_add_time;
        private TextView tv_tip;
        private TextView tv_add_money;
        private LinearLayout ll_surplus_time;
        private TextView tv_fen;
        private Button btn_cancel;
        private Button btn_sumbit;
        private LinearLayout ll_start_distance;
        private LinearLayout ll_location;
        private LinearLayout ll_phone;
        private TextView tv_MorKM;
        private TextView tv_paidui_time;
        TextView tv_HotBox;
        LinearLayout ll_goods;
        TextView tv_goods_money;
        LinearLayout ll_addtime;
        LinearLayout ll_feemoney;
        LinearLayout ll_addmoney;
        TextView tv_goods_type;
        LinearLayout ll_remark;

        public ViewHolder(View itemView) {
            super(itemView);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_total_money = (TextView) itemView.findViewById(R.id.tv_total_money);
            tv_order_type = (TextView) itemView.findViewById(R.id.tv_order_type);
            tv_state = (TextView) itemView.findViewById(R.id.tv_state);
            tv_start_name = (TextView) itemView.findViewById(R.id.tv_start_name);
            tv_end_name = (TextView) itemView.findViewById(R.id.tv_end_name);
            tv_start_distace = (TextView) itemView.findViewById(R.id.tv_start_distance);
            tv_end_distance = (TextView) itemView.findViewById(R.id.tv_end_distance);
            tv_start_addr = (TextView) itemView.findViewById(R.id.tv_start_addr);
            tv_end_addr = (TextView) itemView.findViewById(R.id.tv_end_addr);
            ll_remark = (LinearLayout) itemView.findViewById(R.id.ll_remark);
            tv_remark = (TextView) itemView.findViewById(R.id.tv_remark);
            ll_add_time = (LinearLayout) itemView.findViewById(R.id.ll_add_time);
            tv_add_time = (TextView) itemView.findViewById(R.id.tv_add_time);
            tv_tip = (TextView) itemView.findViewById(R.id.tv_tip);
            tv_add_money = (TextView) itemView.findViewById(R.id.tv_add_money);
            ll_surplus_time = (LinearLayout) itemView.findViewById(R.id.ll_surplus_time);
            tv_fen = (TextView) itemView.findViewById(R.id.tv_fen);
            btn_cancel = (Button) itemView.findViewById(R.id.btn_cancel);
            btn_sumbit = (Button) itemView.findViewById(R.id.btn_sumbit);
            ll_start_distance = (LinearLayout) itemView.findViewById(R.id.ll_start_distance);
            ll_location = (LinearLayout) itemView.findViewById(R.id.ll_location);
            ll_phone = (LinearLayout) itemView.findViewById(R.id.ll_phone);
            tv_MorKM = (TextView) itemView.findViewById(R.id.tv_MorKM);
            tv_paidui_time= (TextView) itemView.findViewById(R.id.tv_paidui_time);
            tv_HotBox= (TextView) itemView.findViewById(R.id.tv_HotBox);
            ll_goods= (LinearLayout) itemView.findViewById(R.id.ll_goods);
            tv_goods_money= (TextView) itemView.findViewById(R.id.tv_goods_money);
            ll_addtime= (LinearLayout) itemView.findViewById(R.id.ll_addtime);
            ll_feemoney= (LinearLayout) itemView.findViewById(R.id.ll_feemoney);
            ll_addmoney= (LinearLayout) itemView.findViewById(R.id.ll_addmoney);
            tv_goods_type= (TextView) itemView.findViewById(R.id.tv_goods_type);
            ll_remark= (LinearLayout) itemView.findViewById(R.id.ll_remark);
        }
    }
    static long time;
    static long hh=0;
    static long mm=0;
    static long ss=0;
    boolean flag=true;
    private  void time(int postion) {
        flag=false;
        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            public void run() {
                time--;
                 hh = time / 60 / 60 % 60;
                 mm = time / 60 % 60;
                 ss = time % 60;
                handler.sendEmptyMessage(0);
            }
        }, 0, 1000);
    }
    static long YZMtime;

    private  void times(String oridId) {
        ChcheUtils.getInstance().getYZMhashMap().put(oridId,false);
        Timer timer = new Timer();
        ChcheUtils.getInstance().getHashMap_time().put(oridId,timer);
        timer.schedule(new TimerTask() {
            public void run() {
                if(ChcheUtils.getInstance().getHashMap_int().get(oridId)==null)
                    return;
                YZMtime=ChcheUtils.getInstance().getHashMap_int().get(oridId);
                YZMtime--;
                handler.sendEmptyMessage(1);
                ChcheUtils.getInstance().getHashMap_int().put(oridId,YZMtime);
            }
        }, 0, 1000);
    }
}
